package com.example.petstoreservice.service;

import com.example.petstoreservice.entity.Cart;
import com.example.petstoreservice.mapper.CartMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class CartService {
    @Autowired
    private CartMapper cartMapper;

    public List<Cart> getByUsername(String username) {
        return cartMapper.getByUsername(username);
    }
    public Cart getCartByIds(String id) {
        return cartMapper.getCartByIds(id);
    }

    public boolean updateQuantityByIds(int quantity, String cartId) {
        int affectedRow = cartMapper.updateQuantityById(quantity, cartId);
        return affectedRow > 0;
    }

    public boolean addCart(Cart cart) {
        // Generate a unique cartId
        cart.setCartId(UUID.randomUUID().toString());
        int affectedRow = cartMapper.addCart(cart);
        return affectedRow > 0;
    }

    public boolean deleteItemById(String cartId) {
        int affectedRow = cartMapper.deleteCartById(cartId);
        return affectedRow > 0;
    }

    public boolean deleteCartByProductIdAndUsername(Long productId, String userName) {
        int affectRow=cartMapper.deleteCartByProductIdAndUsername(productId,userName);
        return affectRow>0;
    }
}
